import { Component, OnInit } from '@angular/core';
import { LayoutRourteStatus } from 'src/app/model/status';
import { MessageService } from 'src/app/core/service';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { MemberService } from 'src/app/routes/member-center/member.server';
import { Router } from '@angular/router';

@Component({
    selector: 'app-edit-psw',
    templateUrl: './edit-psw.component.html',
    styleUrls: ['./edit-psw.component.scss']
})
export class EditPSWComponent implements OnInit {
    resetPwsForm: FormGroup;

    private _layoutRourteStatus: LayoutRourteStatus = {
        url: '/member/center/account',
        isHiddeBreadcrumb: false,
        breadcrumbs: [
            { tragetTitle: '安全设置', breadcrumbUrl: '/member/center/account/setting' },
            { tragetTitle: '修改登陆密码' }
        ]
    };


    ngOnInit() {
        this.msg.sendLayoutRourteStatus(this._layoutRourteStatus);
    }

    confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
        if (!control.value) {
            return { required: true };
        } else if (control.value !== this.resetPwsForm.controls.newPwd.value) {
            return { confirm: true, error: true };
        }
    }

    async submit(data) {
        const tag = await this.memberServer.getAJX('商户修改密码(已登录)', data);
        this.memberServer.getApiSuccess(tag, { bool: true, msg: '密码修改成功，请重新登陆！' }) ? this.successListOk(tag, '商户修改密码(已登录)') : null;
    }
    public successListOk(res, url): void {
        switch (url) {
            case '商户修改密码(已登录)':
                this.router.navigate(['pages/login']);
                break;
        }
    }

    constructor(
        private fb: FormBuilder,
        private memberServer: MemberService,
        private router: Router,
        private msg: MessageService

    ) {
        this.resetPwsForm = this.fb.group({
            oldPwd: [null, [Validators.required]],
            newPwd: [null, [Validators.required]],
            newConfirmPwd: [null, [Validators.required, this.confirmationValidator]],
        });
    }
}
